package model;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for
 * Studentcourse entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see model.Studentcourse
 * @author MyEclipse Persistence Tools
 */

public class StudentcourseDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(StudentcourseDAO.class);
	// property constants
	public static final String STU_ID = "stuId";
	public static final String COURSE_ID = "courseId";
	public static final String TEACHER_ID = "teacherId";
	public static final String XUEQI = "xueqi";

	protected void initDao() {
		// do nothing
	}
	
	/**
	 * 根据学生id，获得该学生的选课信息
	 */
	public List getStudentcourseInfoByStudentId(String stuId)
	{
		String hql="FROM Studentcourse AS stco," +
		"Student AS st,Course AS co,Teacher AS te," +
		"Special AS sp,Department AS de," +
		"Coursetype AS coty " +
		"WHERE stco.stuId=st.id and stco.courseId=co.id and stco.teacherId=te.id" +
		" and st.stuspecialId=sp.id and sp.specialdepartmentId=de.id" +
		" and co.coursetypeId=coty.id";
		String temp="";
		String clause=temp;
		List searchlist=null;
	
		if(stuId!=null&&!"".equals(stuId))
		{
			clause=clause+" and st.id=:stuId";
		}
		if(!clause.equals(temp))
		{
			hql=hql+" "+clause;
		}
		System.out.println("--------------------------");
		System.out.println(hql);
		Query q=super.getSession().createQuery(hql);
		if(stuId!=null&&!"".equals(stuId))
		{
			q.setInteger("stuId",Integer.parseInt(stuId));
		}
		searchlist=q.list();
	
	return searchlist;
	}
	
	public void deleteAllStudentcourseInfo()
	{
		String hql="DELETE FROM Studentcourse AS sc";
		Query q=super.getSession().createQuery(hql);
		q.executeUpdate();
	}
	
	/**
	 * 根据选课表id获得信息
	 * @param studentcourseId
	 * @return
	 */
	public List getStudentcourseInfoByStudentcourseId(String studentcourseId)
	{
		String hql="FROM Studentcourse AS stco," +
		"Student AS st,Course AS co,Teacher AS te," +
		"Special AS sp,Department AS de," +
		"Coursetype AS coty " +
		"WHERE stco.stuId=st.id and stco.courseId=co.id and stco.teacherId=te.id" +
		" and st.stuspecialId=sp.id and sp.specialdepartmentId=de.id" +
		" and co.coursetypeId=coty.id";
	String temp="";
	String clause=temp;
	List searchlist=null;
	
		if(studentcourseId!=null&&!"".equals(studentcourseId))
		{
			clause=clause+" and stco.id=:studentcourseId";
		}
		if(!clause.equals(temp))
		{
			hql=hql+" "+clause;
		}
		System.out.println("--------------------------");
		System.out.println(hql);
		Query q=super.getSession().createQuery(hql);
		if(studentcourseId!=null&&!"".equals(studentcourseId))
		{
			q.setInteger("studentcourseId",Integer.parseInt(studentcourseId));
		}
		searchlist=q.list();
	
	return searchlist;
	}
	
	//get all studentcourse info
	public List getAllStudentcourseInfo(String rightsql)
	{
		String hql="FROM Studentcourse AS stco," +
				"Student AS st,Course AS co,Teacher AS te," +
				"Special AS sp,Department AS de," +
				"Coursetype AS coty " +
				"WHERE stco.stuId=st.id and stco.courseId=co.id and stco.teacherId=te.id" +
				" and st.stuspecialId=sp.id and sp.specialdepartmentId=de.id" +
				" and co.coursetypeId=coty.id";
		if(!rightsql.equals(""))
			hql=hql+" and "+rightsql;
		Query q=super.getSession().createQuery(hql);
		List list = q.list();
		return list;
	}
	
	//按学生或课程信息搜索学生选课信息
	public List searchStudentcourses(String stunumber,String stuname,String stusex,String stuspecialId,String specialdepartmentId,String coursename,String coursetypeId,String rightsql)
	{
		String hql="FROM Studentcourse AS stco," +
			"Student AS st,Course AS co,Teacher AS te," +
			"Special AS sp,Department AS de," +
			"Coursetype AS coty " +
			"WHERE stco.stuId=st.id and stco.courseId=co.id and stco.teacherId=te.id" +
			" and st.stuspecialId=sp.id and sp.specialdepartmentId=de.id" +
			" and co.coursetypeId=coty.id";
		String temp="";
		String clause=temp;
		List searchlist=null;
		
			if(stunumber!=null&&!"".equals(stunumber))
			{
				clause=clause+" and st.stunumber like :stunumber";
			}
			if(stuname!=null&&!"".equals(stuname))
			{
				clause=clause+" and st.stuname like :stuname";
			}
			if(stusex!=null&&!"".equals(stusex)&&!stusex.equals("0"))
			{
				clause=clause+" and st.stusex=:stusex";
			}
			if(stuspecialId!=null&&!"".equals(stuspecialId)&&!stuspecialId.equals("0"))
			{
				clause=clause+" and st.stuspecialId=:stuspecialId";
			}
			if(specialdepartmentId!=null&&!"".equals(specialdepartmentId)&&!specialdepartmentId.equals("0"))
			{
				clause=clause+" and de.id=:specialdepartmentId";
			}
			if(coursename!=null&&!"".equals(coursename))
			{
				clause=clause+" and co.coursename like :coursename";
			}
			if(coursetypeId!=null&&!"".equals(coursetypeId))
			{
				clause=clause+" and coty.coursetypeId=:coursetypeId";
			}
			
			if(!clause.equals(temp))
			{
				hql=hql+" "+clause;
			}
			if(!rightsql.equals(""))
				hql=hql+" and "+rightsql;
			System.out.println("--------------------------");
			System.out.println(hql);
			Query q=super.getSession().createQuery(hql);
			if(stunumber!=null&&!"".equals(stunumber))
			{
				q.setString("stunumber", '%'+stunumber+'%');
			}
			if(stuname!=null&&!"".equals(stuname))
			{
				q.setString("stuname", '%'+stuname+'%');
			}
			if(stusex!=null&&!"".equals(stusex)&&!stusex.equals("0"))
			{
				q.setString("stusex", stusex);
			}
			if(stuspecialId!=null&&!"".equals(stuspecialId)&&!stuspecialId.equals("0"))
			{
				q.setInteger("stuspecialId", Integer.parseInt(stuspecialId));
			}
			if(specialdepartmentId!=null&&!"".equals(specialdepartmentId)&&!specialdepartmentId.equals("0"))
			{
				q.setInteger("specialdepartmentId", Integer.parseInt(specialdepartmentId));
			}
			if(coursename!=null&&!"".equals(coursename))
			{
				q.setString("coursename", '%'+coursename+'%');
			}
			if(coursetypeId!=null&&!"".equals(coursetypeId))
			{
				q.setString("coursetypeId", coursetypeId);
			}
			searchlist=q.list();
		
		return searchlist;
	}
	
	public void save(Studentcourse transientInstance) {
		log.debug("saving Studentcourse instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Studentcourse persistentInstance) {
		log.debug("deleting Studentcourse instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Studentcourse findById(java.lang.Integer id) {
		log.debug("getting Studentcourse instance with id: " + id);
		try {
			Studentcourse instance = (Studentcourse) getHibernateTemplate()
					.get("model.Studentcourse", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Studentcourse instance) {
		log.debug("finding Studentcourse instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Studentcourse instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from Studentcourse as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByStuId(Object stuId) {
		return findByProperty(STU_ID, stuId);
	}

	public List findByCourseId(Object courseId) {
		return findByProperty(COURSE_ID, courseId);
	}

	public List findByTeacherId(Object teacherId) {
		return findByProperty(TEACHER_ID, teacherId);
	}

	public List findByXueqi(Object xueqi) {
		return findByProperty(XUEQI, xueqi);
	}

	public List findAll() {
		log.debug("finding all Studentcourse instances");
		try {
			String queryString = "from Studentcourse";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Studentcourse merge(Studentcourse detachedInstance) {
		log.debug("merging Studentcourse instance");
		try {
			Studentcourse result = (Studentcourse) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Studentcourse instance) {
		log.debug("attaching dirty Studentcourse instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Studentcourse instance) {
		log.debug("attaching clean Studentcourse instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static StudentcourseDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (StudentcourseDAO) ctx.getBean("StudentcourseDAO");
	}
}